/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lardocelar.core.comunicacaocaptador;

import br.com.lardocelar.core.Captador;
import br.com.lardocelar.core.ComunicacaoCaptador;
import br.com.lardocelar.core.IpUtil;
import br.com.lardocelar.core.Mensagem;
import br.com.lardocelar.core.MensagemInvalida;
import br.com.lardocelar.core.SituacaoComunicacaoCaptador;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LeandroL
 */
public class UDP implements ComunicacaoCaptador{

    @Override
    public String getId(){
        return "01";
    }
    
    @Override
    public SituacaoComunicacaoCaptador getSituacaoComunicacao(Captador captador){
        SituacaoComunicacaoCaptador retorno = SituacaoComunicacaoCaptador.OFFLINE;
        String s;
        List<String> commands = new ArrayList<>();
        commands.add("ping");
        commands.add(IpUtil.parseIpaddressToString(captador.getIpAddress()));
        ProcessBuilder processbuilder = new ProcessBuilder(commands);
        Process process;
        try {
            InetAddress inet = InetAddress.getByAddress(captador.getIpAddress());
            boolean reachable = inet.isReachable(5000);
            if(reachable) retorno = SituacaoComunicacaoCaptador.ONLINE;
//            process = processbuilder.start();
//        
//            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
//            System.out.println("Here is the standard output of the command:\n");
//            while ((s = stdInput.readLine()) != null){
//                  System.out.println(s);
//            }
//            retorno = SituacaoComunicacaoCaptador.ONLINE;
        } catch (IOException ex) {
            Logger.getLogger(UDP.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return retorno;
    }
    
    /**
     *
     * @param captador
     * @param mensagem
     * @return
     * @throws SocketException
     * @throws UnknownHostException
     * @throws IOException
     * @throws MensagemInvalida
     */
    @Override
    public Mensagem Comunicar(Captador captador, Mensagem mensagem) throws SocketException, UnknownHostException, IOException, MensagemInvalida {
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByAddress(captador.getIpAddress());

        byte[] receiveData = new byte[1024];
        byte[] sendData = mensagem.gerarMensagem().getBytes();

        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, captador.getPort());
        clientSocket.send(sendPacket);

        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        String modifiedSentence = new String(receivePacket.getData());

        return Mensagem.tranformarMensagem(modifiedSentence);
    }
}
